package cn.edu.ctbu.secondclass.service;

import cn.edu.ctbu.secondclass.domain.Student;
import cn.edu.ctbu.secondclass.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class StudentService {
    @Autowired
    private StudentRepository studentRepository;
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    public Student findById(Integer id){



        return studentRepository.findById(id).orElse( null);
    }

    public List<Student> findByName(String name){


        return studentRepository.findByNameLike(name);
    }
    public List<Student> findByNameLike(String name){

        return studentRepository.findByNameLike(name);
    }
    /*
    按名字和密码查询
     */
    public List<Student> findByNameAndPassword(String name, String password){


        return studentRepository.findByNameAndPassword(name,password);
    }

    public Student insert(Student student){
        studentRepository.save(student);
        return student;
    }
    public Student update(Student student){
        studentRepository.save(student);
        return student;
    }

    /**
     * 删除
     * @param
     */


    public Student delete(Student student){
        studentRepository.delete(student);
        return student;

    }

//    public void delete(Integer id){
//
//        studentRepository.deleteById(id);
//    }
    public void delete(Integer id){
        Student student=new Student();
        student.setId(id);
        studentRepository.delete(student);
    }

    /**
     * 查询最大工号
     * @return
     */
    public List<Student> getTeacherByMaxNo(){

        return studentRepository.getTeachersByMaxNo();
    }
    public Student getById(Integer id){
        Student student=studentRepository.findById(id).orElse(null);
        return student;
    }

}
